package games.tankwars.behaviors;

import vooga.core.AbstractLevel;
import vooga.entities.Entity;


/**
 * @author Robert C. Duvall
 */
public class TankFollow extends vooga.behaviors.AbstractBehavior
{
    private games.tankwars.entities.Tank myTank;
    private boolean movingX;
    private boolean movingY;
    private AbstractLevel myLevel;
    private int myTimer;
    private int myFollowSpeed;
    
    public TankFollow (games.tankwars.entities.Tank tank, int followSpeed, AbstractLevel level)
    {
        myTank = tank;
        movingY=false;
        movingX=false;
        myTank = tank;
        myLevel=level;
        myTimer=0;
        myFollowSpeed=(5-followSpeed)*40;
    }


	@Override
	public void step(Entity target) {
		// TODO Auto-generated method stub
		boolean confused=false;
		myTimer++;
		
		double dx=myTank.getCenter().getX()-target.getCenter().getX();
		double dy=myTank.getCenter().getY()-target.getCenter().getY();
		int random = (int) Math.round(Math.random());
		
		if (Math.abs(dx)>Math.abs(dy))
			movingX=true;
		else
			movingX=false;
			
		movingY=!movingX;
		if (Math.abs(dx)==Math.abs(dy))
			confused=true;
		
		int heading=180;
		double displacement=-1;


		System.out.println(random);
		
		if (confused)
			dx=dx+(int)(Math.random()*100)%13;
			dx=dx+(int)(Math.random()*100)%13;
		
		if (myTank.isVisible()) 
		{
			if (dx > 0 && movingX) 
			{
				heading = 0;
				displacement *= -1;
			} 
			else if (dy > 0 && movingY) 
			{
				heading = 90;
				displacement *= -1;
			}
			else if (dy < 0 && movingY)
				heading = 270;

			target.setHeading(heading);	

			if (!confused)
				if (movingX)
					target.translate(displacement,0);
				else
					target.translate(0,displacement);
		}	
	}
}
